package wqh.controller.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import wqh.domain.User;
import wqh.domain.UserRole;
import wqh.global.GlobalRole;
import wqh.global.GlobalSession;
import wqh.service.UserRoleService;
import wqh.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/user_login")
public class UserView {

    private static Logger logger = LoggerFactory.getLogger(UserView.class);

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService roleService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView login(HttpServletRequest httpServletRequest,
                              @RequestParam(value = "user_name",required = true) String user_name,
                              @RequestParam(value = "user_password",required = true) String user_password)
    {
      try {

          User user = new User();
          user.setUser(user_name);
          user.setPassword(user_password);
          List<User> result = userService.findAllByObject(user);
          if(result.size() != 0)
          {
              String session_id = httpServletRequest.getSession().getId();
              GlobalSession.addSession(user_name,session_id);
              System.out.println("添加SESSION ID: " + session_id);
              UserRole role = roleService.findById(result.get(0).getRole_id());
              if(role == null){
                  ModelAndView m = new ModelAndView("ftl_login");
                  m.addObject("user",user_name);
                  m.addObject("error","此用户没有权限，请联系管理员解决此问题。");
                  return m;
              }
              GlobalRole.addRole(user_name,role.getName());
              return new ModelAndView("redirect:/view/index");
          }
          else
          {
              ModelAndView m = new ModelAndView("ftl_login");
              m.addObject("user",user_name);
              m.addObject("error","登录失败,请检查用账号和密码。");
              return m;
          }
      }catch (Exception e)
      {
          e.printStackTrace();
          logger.error("登录错误：" + e);
          ModelAndView m = new ModelAndView("ftl_login");
          m.addObject("error","系统错误，请联系管理员。");
          return m;
      }
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest httpServletRequest)
    {
        String session_id = httpServletRequest.getSession().getId();
        String userName = GlobalSession.removeSessionBySessionIDGetName(session_id);
        GlobalRole.removeRoleByUserName(userName);
        return new ModelAndView("ftl_login");
    }

}
